package com.probiz.estore.product.service.impl;

import java.util.List;

import com.probiz.estore.attribute.dao.ProductAttrValueDao;
import com.probiz.estore.common.model.attribute.AttributeValue;
import com.probiz.estore.common.model.attribute.ProductAttrValue;
import com.probiz.estore.common.model.product.Product;
import com.probiz.estore.common.model.product.ProductAttGroupItem;
import com.probiz.estore.common.service.AttributeService;
import com.probiz.estore.core.service.impl.GenericManagerImpl;
import com.probiz.estore.product.service.ProductAttGroupItemManager;
import com.probiz.estore.product.service.ProductAttrValueManager;


/**
 * Manager implementation for ProductAttrValue, responsible for business processing, and communicate between web and persistence layer.
 */
public class ProductAttrValueManagerImpl extends GenericManagerImpl<ProductAttrValue> implements ProductAttrValueManager {

	private ProductAttrValueDao productAttrValueDao = null;
	private ProductAttGroupItemManager productAttGroupItemManager = null;
	private AttributeService attributeService = null;
	/**
	 * @param productAttrValueDao
	 *            the productAttrValueDao to set
	 */
	public void setProductAttrValueDao(ProductAttrValueDao productAttrValueDao) {
		this.productAttrValueDao = productAttrValueDao;
	}

	public void setProductAttGroupItemManager(
			ProductAttGroupItemManager productAttGroupItemManager) {
		this.productAttGroupItemManager = productAttGroupItemManager;
	}

	public AttributeService getAttributeService() {
		return attributeService;
	}

	public void setAttributeService(AttributeService attributeService) {
		this.attributeService = attributeService;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.service.impl.GenericManagerImpl#init()
	 */
	@Override
	protected void initManager() {
		dao = productAttrValueDao;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.service.impl.GenericManagerImpl#onDelete(java.lang.Object)
	 */
	@Override
	protected void preDelete(ProductAttrValue entity) {

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.service.impl.GenericManagerImpl#onSave(java.lang.Object)
	 */
	@Override
	protected void preSave(ProductAttrValue entity) {

	}

	
	
	
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.product.service.ProductAttrValueManager#saveProductAttrValueFromRequest(com.probiz.estore.product.model.Product, java.util.List)
	 */
	public void saveProductAttrValueFromRequest(Product product,List<AttributeValue> attributeValues){
		
		for (AttributeValue attributeValue : attributeValues) {
			ProductAttrValue productAttrValue = (ProductAttrValue) attributeValue;
			if (productAttrValue.getId() == null) {
				//start:判断是否“真”新增
				ProductAttrValue existProductAttrValue = productAttrValueDao.getProductAttrValue(product.getProductId(),productAttrValue.getAttributeId());
				//end:判断是否“真”新增
				if(existProductAttrValue == null){
					//case:真“新增”
					// 为新增的自定义属性指定ProductAttGroupItem关联
					ProductAttGroupItem productAttGroupItem = productAttGroupItemManager
							.getProductAttGroupItemByProductTypeAndAttribute(
									product.getProductTypeId(),
									productAttrValue.getAttributeId());
					productAttrValue
							.setProductAttGroupItem(productAttGroupItem);
				}else{
					//case:假“新增”：出现假新增的原因在于后台save是用ajax提交，在属性第一次保存后，新增的id并没有set回页面中(！！目前全部为假真增)
					this.evict(existProductAttrValue);
					productAttrValue.setProductAttrValueId(existProductAttrValue.getProductAttrValueId());
					productAttrValue.setProductAttGroupItemId(existProductAttrValue.getProductAttGroupItemId());
				}
			}
		}
		// 保存、更新自定义属性
		attributeService.saveOrUpdateAttributeValue(attributeValues, product);
	}
}
